package com.neusoft.ehr.controller;

import com.neusoft.ehr.service.DeptService;
import com.neusoft.ehr.service.EmpService;
import com.neusoft.entity.Dept;
import com.neusoft.entity.Emp;
import com.sun.deploy.model.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin("*")
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    EmpService empService;
    @GetMapping("/a")
    public  String  f2(   ) throws FileNotFoundException {
        URL url = ResourceUtils.getURL("classpath:static");
        System.out.println( url.getPath());
        return  url.getPath();
    }

    @PostMapping("/add")
    public  String  f1( Emp emp , MultipartFile file ) throws IOException {
        UUID uuid =  UUID.randomUUID();
        String filename =  uuid.toString() + file.getOriginalFilename().substring( file.getOriginalFilename().indexOf("."));
        emp.setIcon(filename);


        String path =  new ClassPathResource("static/").getFile().getPath();
        path = path +"\\" +  filename;
        File tofile =new File(path);
        if (tofile.exists()){

        }else {
            tofile.createNewFile();
        }
        file.transferTo(tofile);

        int i = empService.addEmp(emp);

        String r  = i ==1? "ok" :"error";

        return  r ;
    }
}
